package com.fbee.smartcommunities.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.fbee.smartcommunities.entity.User;

import java.util.Date;

/**
 * ClassName: com.fbee.smartcommunities.utils
 * Datetime: 2024/8/16 下午4:32
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
public class JwtUtil {

    private static final String KEY = "FBee";

    /**
     * 获得Token
     * @param
     * @return
     */
    public static String create(User user){
        return JWT.create()
                .withClaim("id",user.getId())
                .withClaim("name",user.getName())
                .withClaim("time",new Date())
                .sign(Algorithm.HMAC256(KEY));
    }

    public static Long decode(String jwt){
        try {
            return JWT.require(Algorithm.HMAC256(KEY))
                    .build()
                    .verify(jwt)
                    .getClaim("id").asLong();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new CustomizeException("非法请求");
        }
    }


}
